<template>
	<view class="navbar">
		<view class="navbar-position-fixed">
			<!--状态栏-->
			<view :style="{height:statusBarHeight+'px'}"></view>
			<!--导航栏-->
			<view class="navbar-content" :class="{search:isSearch}" :style="{height:navbarheight+'px',width:windowWidth+'px'}"  @click.stop="open">
				
				<view v-if="!isSearch" class="navbar-search">
					<!-- 非搜索页显示 -->
					<view class="navbar-search_icon">
						<uni-icons type="search" size="16" color="#999"></uni-icons>
					</view>
					<view class="navbar-search_text">uni-app、vue</view>
				</view>	 
					<view v-if="isSearch" class="navbar-search-icon-back" @click="back">
						<uni-icons type="back" size="22" color="#fff"></uni-icons>
					</view>
					<view v-if="isSearch" class="navbar-search">
					<!-- 搜索页显示 -->
					 <input class="navbar-search_text" type="text"  v-model="val" @input="inputChange"
					   placeholder="请输入搜索的内容" /> 
					</view>	
				 			
			</view>
			
		</view>	
		<!--占位高度=状态栏高度+导航栏高度-->
		<view :style="{height:statusBarHeight+navbarheight+'px'}"></view>
	</view>
</template>

<script>
	export default {
		props:{
			isSearch:{
				type:Boolean,
				default:false
			},
			value:{
				type:[Number,String],
				default:''
			}
		},
		data() {
			return {
				statusBarHeight:20,
				navbarheight:45,
				windowWidth:375,
				val:''
			};
		},
		watch:{
			value(newval){
				this.val=this.value
			}
		},
		created() {
		//获取手机系统信息
		const info=	uni.getSystemInfoSync()
		//设置状态栏高度
		this.statusBarHeight=info.statusBarHeight
		this.windowWidth=info.windowWidth
		 
		 //H5 App mp-alipay
		 // #ifndef H5 || APP-PLUS || MP-ALIPAY
		//获取胶囊的位置
		const MenuButtoninfo=uni.getMenuButtonBoundingClientRect()
		//(胶囊底部高度-状态栏高度)+(胶囊顶部-状态栏高度),一般改版后都需要+4
		this.navbarheight=(MenuButtoninfo.bottom-info.statusBarHeight)+(MenuButtoninfo.top-info.statusBarHeight)+4
		this.windowWidth=MenuButtoninfo.left
		//#endif
		},
		methods:{
			open(){
				if(this.isSearch) return
				uni.navigateTo({
					url:"/pages/home-search/home-search"
				})
			},
			back(){
				uni.switchTab({
					url:'/pages/tabbar/index/index'
				})
			},
			//搜索栏输入数据触发事件
			inputChange(e){
				const {value}=e.detail
				this.$emit("input",value)				
			}
		}
	}
</script>

<style lang="scss">
	.navbar {
		.navbar-position-fixed{
			position: fixed ; /* 将导航栏始终定位在最上层*/
			top: 0;
			left: 0;
			z-index: 99;
			width: 100%;
			background-color: $mk-base-color;	
					
			.navbar-content{
				display: flex;  /* 设置内容弹性布局，水平垂直居中*/
				justify-content: center;
				align-items: center;
				padding: 0 15px;				
				height: 45px;
				box-sizing: border-box; /* 让navbar-search在盒内显示*/
				
				.navbar-search{
					display: flex;
					align-items: center;
					padding: 0 15px;
					width: 100%;
					height: 30px;
					border-radius: 30px; /* 圆角设置*/
					background-color: #FFFFFF;
					.navbar-search_icon{						  
						margin-right: 10px;
					}
					.navbar-search_text{
						width: 100%;
						font-size: 14px;
						color: #999;
					}
				}
				&.search{
					padding-left: 0;
					.navbar-search-icon-back{
						margin-left: 10px;
						margin-right:10px ;
					}
					.navbar-search{
						border-radius: 5px;
					}
					
				}
			}
			
		}
		
	}
</style>
